package com.oyjd.business.listview.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.oyjd.R;
import com.oyjd.business.listview.service.ChengJiService;
import com.oyjd.business.listview.util.CountUtil;
import com.oyjd.framework.Const;
import com.oyjd.framework.ui.title.TitleBar;
import com.oyjd.framework.util.MsgShow;

/**
 * 新增成绩
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class ChengJiEditActivity extends Activity {
	private TitleBar titleBar;
	private EditText et_title;
	private EditText et_content;
	private Button bt_preview;
	private Button bt_save;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_edit_cj);

		// 界面元素初始化
		titleBar = (TitleBar) this.findViewById(R.id.titleBar);
		titleBar.setBackVisiable(false);
		titleBar.setRightVisiable(false);
		titleBar.setTitleText("新增成绩");

		et_title = (EditText) this.findViewById(R.id.et_title);

		et_content = (EditText) this.findViewById(R.id.et_content);

		bt_preview = (Button) this.findViewById(R.id.bt_preview);
		bt_preview.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final String c = et_content.getText().toString().trim();
				if ("".equals(c)) {
					MsgShow.showShortToast(ChengJiEditActivity.this, "没有输入任何成绩");
					return;
				}

				Intent intent = new Intent(ChengJiEditActivity.this, ShowCountActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("fileName", "预览统计");
				bundle.putString("msg", CountUtil.count(c));
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});

		bt_save = (Button) this.findViewById(R.id.bt_save);
		bt_save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final String c = et_content.getText().toString().trim();
				String t = et_title.getText().toString().trim();
				if ("".equals(c)) {
					MsgShow.showShortToast(ChengJiEditActivity.this, "没有输入任何成绩");
					return;
				}
				if ("".equals(t)) {
					t = getResources().getString(R.string.evTitle);
				}
				final String title = t;

				Builder builder = new AlertDialog.Builder(ChengJiEditActivity.this);
				builder.setTitle("提示信息");
				builder.setMessage("[保存退出]后不提供再修改功能，请先确保[预览统计]结果达到您的预期！");
				builder.setPositiveButton("保存退出", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface d, int i) {
						ChengJiService.saveFile(title, c);
						Intent intent = new Intent();
						intent.putExtra("msg", "my back");
						setResult(Const.ChengJiEditActivity_code, intent);
						finish();
					}
				});

				builder.setNeutralButton("继续编辑", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface d, int i) {

					}
				});
				AlertDialog dialog = builder.create();
				dialog.show();

			}
		});
	}

	// 用于监听手机的退出事件，彻底退出程序
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Builder builder = new AlertDialog.Builder(ChengJiEditActivity.this);
			builder.setTitle("提示信息");
			builder.setMessage("确定直接退出编辑页面吗？");
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface d, int i) {
					ChengJiEditActivity.this.finish();
				}
			});

			builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface d, int i) {

				}
			});
			AlertDialog dialog = builder.create();
			dialog.show();
			return true;
		}
		return false;
	}

}
